//回车登录
window.onkeydown = function (evt) {
    let e = evt || window.event;
    if (e.keyCode == "13") {
		$('#login-btn').click();
	}
}


//点击提示弹框关闭
$('.message-close-btn').on('click', function () { 
    $('.message1').css('display','none')
})

//登录接口调用

function startLogin(obj) {

    let username = document.querySelector('.userData').value;
    let password = document.querySelector('.pasData').value;
    let errImg = document.querySelector('.out-img');
    let mesData = document.querySelector('.message');
    console.log(username,password)
    if (username == '' || password == ''){ 
        $('.message1').css('display','flex')
        return
    }

    if (obj.innerText == '注册') {
        regLogin();
    } else { 
        let getUrl = 'http://jx.xuzhixiang.top/ap/api/login.php';
        let r1 = axios.get(getUrl, { params: { username, password } })
        r1.then((data) => { 
            console.log(data)
            console.log(data.data.msg)
            if (data.data.code == 1) {
                localStorage.setItem('username', username);
                localStorage.setItem("id", data.data.data.id);
                localStorage.setItem("token", data.data.data.token);
                localStorage.setItem('password', password)
                // alert(data.data.msg);
                $('.message1').css('display', 'flex');
                $('.mes-content').text(data.data.msg) 
                timerId;
                location.href = '../index.html'
                // window.open('../index.html')
            } else { 
                errImg.style.display = 'block';
                mesData.innerText = data.data.msg;
            }
        })
    }
}


//调用注册接口
function regLogin() { 
    
    let username = document.querySelector('.userData').value;
    let password = document.querySelector('.pasData').value;
    let loginUrl = "http://jx.xuzhixiang.top/ap/api/reg.php";
    let p1 = axios.get(loginUrl, { params: { username, password } });
    p1.then((data) => { 
        // console.log(data);
        if (data.data.code == 1) {
            // alert(data.data.msg)
            $('.message1').css('display', 'flex');
            $('.mes-content').text(data.data.msg) 
            // var timerId = setTimeout(function () {
            //     $('.message1').css('display', 'none');
            // }, 3000)
            timerId;
            let getUrl = 'http://jx.xuzhixiang.top/ap/api/login.php';
            let r1 = axios.get(getUrl, { params: { username, password } })
            r1.then((data) => { 
                console.log(data)
                console.log(data.data.msg)
                if (data.data.code == 1) {
                    localStorage.setItem('username', username);
                    localStorage.setItem("id", data.data.data.id);
                    localStorage.setItem("token", data.data.data.token);
                    localStorage.setItem('password', password)
                    // alert(data.data.msg);
                    location.href = '../index.html'
                    // window.open('../index.html')
                } else { 
                    $('.out-img').css('display','block')
                    $('.message').text(data.data.msg)
                }
            })
        } else { 
            $('.out-img').css('display','block')
            $('.message').text(data.data.msg)
        }
        //模拟点击登录选项
        // $('.box-left').trigger('click');
    })

}
//提示信息显示和隐藏
username.oninput = function () {
    let outShow = document.querySelector('.out-img');
    let mesData = document.querySelector('.message');
    outShow.style.display = 'none';
    mesData.innerText = '';
}

// reguser.oninput = function () {
//     let outShow = document.querySelector('.out-img1');
//     let mesData = document.querySelector('.message1');
//     outShow.style.display = 'none';
//     mesData.innerText = '';
// }
//记住密码

function btnbox(evt) {
    
    let e = evt || window.event;
    // console.log(e.target.checked);
    let result = e.target.checked;

    localStorage.setItem('checked', result)

    let getLocal = localStorage.getItem('checked');
    let str = JSON.parse(getLocal)
    // console.log(str)

    let username = document.querySelector('.userData').value;
    let password = document.querySelector('.pasData').value;
    localStorage.setItem('username', username)
    localStorage.setItem('username1', username)
    localStorage.setItem('password', password)

    if (str == false) {
        $('#record').removeProp('checked');
        localStorage.clear();
    } else { 
        fu();
    }
    
}

function fu() {
    let getLocal = localStorage.getItem('checked');
    let password = document.querySelector('.pasData');
    let str = JSON.parse(getLocal)
    if (str == true) {
        $('#record').prop('checked', 'checked')
        let getUser = localStorage.getItem('username1');
        let getPass = localStorage.getItem('password');
        username.value = getUser;
        password.setAttribute('value', getPass)
    } else { 
        localStorage.clear();
    }
}
fu();

// 更换注册

let sign = document.querySelector('.box-right');
// console.log(sign.classList.contains('selected'))
let reg = document.querySelector('.btn');
let datalist = document.querySelectorAll('.everyone');
let showImg = document.querySelector('.show-img');
console.log(showImg)
console.log(datalist)
datalist.forEach((item)=>{ 
    item.onclick = () => { 
        // let logData = document.querySelector('.log');
        let username = document.querySelector('.userData');
        let password = document.querySelector('.pasData');
        // let enr = document.querySelector('.enroll');

        for (let i = 0; i < datalist.length; i++) { 
            datalist[i].classList.remove('selected')
        }
        item.classList.add('selected')
        if (sign.classList.contains('selected') == false) {
            // item.classList.add('selected')
            
            // logData.style.display = 'block'
            // enr.style.display = 'none'
            reg.innerText = '登录';
            showImg.style.display = 'block'
            username.setAttribute('placeholder','邮箱/手机号码')
            password.setAttribute('placeholder','密码')
        }else {
            username.setAttribute('placeholder','请输入注册邮箱/手机号')
            password.setAttribute('placeholder','请输入注册密码')
            // enr.style.display = 'block'
            reg.innerText = '注册';
            showImg.style.display = 'none';
        }
    }
})

//点击图片旋转

$('.img-check-pics').find('li').find('img').on('click', function () { 
    // console.log($(this).parent().css('marginTop'))
    let orig = parseInt(window.getComputedStyle(this).marginTop)
    console.log(orig)
    if (orig == '-180') { 
        this.style.marginTop = '0px';
        return
    }
    this.style.marginTop = orig +(-60) +'px';
})


//定时器
var timerId = setTimeout(function () {
    $('.message1').css('display', 'none');
}, 3000)